package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.User;
import com.cskaoyan.bo.RegisterBo;
import com.cskaoyan.bo.UserInfo;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.constants.MallConstant;
import com.cskaoyan.exception.GlobalException;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.WxUserAuThService;
import com.cskaoyan.shiro.MallToken;
import com.cskaoyan.utils.RandomUtil;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.UserAuthBaseRespVo;
import com.cskaoyan.vo.UserAuthVo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
public class WxAuthController {

    @Autowired
    WxUserAuThService wxUserAuThService;
    @Autowired
    AliyunComponent aliyunComponent;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserMapper userMapper;
    @RequestMapping("wx/auth/login")
    public BaseRespVo login(@RequestBody Map map){
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        MallToken token = new MallToken(username, password, "wx");
        //认证
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        String id = (String) subject.getSession().getId();
        //授权
        String avatarUrl = wxUserAuThService.selectAvatarUrl(username);
        UserInfo userInfo = new UserInfo(username, avatarUrl);
        UserAuthVo userAuthVo = new UserAuthVo(userInfo,new Date(),id);
        return BaseRespVo.ok(userAuthVo);
    }
    //退出
    @PostMapping("wx/auth/logout")
    public BaseRespVo logout(){
        SecurityUtils.getSubject().logout();
        return BaseRespVo.ok();
    }

    //用户首页
//    @GetMapping("wx/user/index")
//    public BaseRespVo indexUser(){
//
//        return null;
//    }

        @PostMapping("wx/auth/register")
    public BaseRespVo userRegister(@RequestBody RegisterBo registerBo) {
//
////        LoginVo_wx loginVo_wx = wxService.handlerUserRegister(userRegisterVO);
////
////        return BaseRespVo.ok(loginVo_wx);

     //判断验证码是否正确
            String code = stringRedisTemplate.opsForValue().get(registerBo.getMobile());
            if (!Objects.equals(code, registerBo.getCode())) {
                throw new GlobalException(501, MallConstant.USER_REGISTER_CODE_ERR);
            }

//            String username = registerBo.getUsername();
//            String password = registerBo.getPassword();
//            User user = new User();
//            //加密密码
//            String enPassword = MD5.EnCode(password);
//            user.setUsername(username);
//            user.setPassword(enPassword);
//            user.setGender((byte) 0);
//            user.setUserLevel((byte) 0);
//            user.setNickname(registerBo.getMobile());
//            user.setMobile(registerBo.getMobile());
//            user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
//            user.setStatus((byte) 0);
//            user.setAddTime(new Date());
//            user.setUpdateTime(new Date());
//            user.setDeleted(false);
//            user.setNickname(registerBo.getUsername());
//            user.setLastLoginIp("1.1.1.1");
//            user.setBirthday(new Date());
//            int insert = userMapper.insert(user);
//            MallToken mallToken = new MallToken(username, password, "wx");
//            Subject subject = SecurityUtils.getSubject();
//            subject.login(mallToken);
//            String id = (String) subject.getSession().getId();
//            String avatarUrl = wxUserAuThService.selectAvatarUrl(username);
//
//            UserInfo userInfo = new UserInfo(username, avatarUrl);
//            UserAuthVo userAuthVo = new UserAuthVo(userInfo,new Date(),id);
//            return BaseRespVo.ok(userAuthVo);

//            String enPassword = MD5.EnCode(registerBo.getPassword());
//            registerBo.setPassword(enPassword);
            boolean b =  wxUserAuThService.registerUser(registerBo);
            String username = registerBo.getUsername();
            String password = registerBo.getPassword();
           // String password=enPassword;
            MallToken mallToken = new MallToken(username, password, "wx");

            Subject subject = SecurityUtils.getSubject();

            subject.login(mallToken);

            String id = (String) subject.getSession().getId();

            String avatarUrl =  wxUserAuThService.selectAvatarUrl(username);

            UserInfo userInfo = new UserInfo(username, avatarUrl);

            UserAuthVo userAuthVo = new UserAuthVo(userInfo,new Date(),id);

             return BaseRespVo.ok(userAuthVo);


    }



    @RequestMapping("wx/auth/regCaptcha")
    public UserAuthBaseRespVo sendMobile(@RequestBody Map<String, Object> map) {
        //生成随机数
        String code = RandomUtil.getFourBitRandom();
        Map<String, Object> param = new HashMap<>();
        //把验证码存到Map中
        param.put("code", code);
        //获得前端的发来的手机号码
        String phoneNum = String.valueOf(map.get("mobile"));
        if (Objects.isNull(phoneNum)) {
            throw new GlobalException(501, MallConstant.MSG_SEND_PHONE_ERR);
        }
        //发送短信
        aliyunComponent.sendMsg(phoneNum,code );
        //把验证码和手机号存到redis中，时间为2分钟
        stringRedisTemplate.opsForValue().set(phoneNum,code,10, TimeUnit.MINUTES);
        return UserAuthBaseRespVo.ok();

    }
}
